<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客编辑</title>

    <!-- 引入自己写的样式 -->
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_edit.css">

    <!-- 引入 editor.md 的依赖 -->
    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="js/common.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/system.png">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="person.html">资料修改</a>
        <a href="javascript: logout()">退出登录</a>
    </div>
    <!-- 编辑框容器 -->
    <div class="blog-edit-container">
        <!-- 标题编辑区 -->
        <div class="title">
            <input type="text" id="title" placeholder="在这里写下文章标题">&nbsp&nbsp
            <button onclick="save()">保存草稿</button>&nbsp&nbsp
            <button onclick="time()">定时发布</button>&nbsp&nbsp
            <input type="text" id="releasetime" style="width: 420px;" placeholder="格式如:2/30表示两小时三十分钟后发布">&nbsp&nbsp
            <button onclick="mysub()">发布文章</button>
        </div>
        <!-- 创建编辑器标签 -->
        <div id="editorDiv">
            <textarea id="editor-markdown" style="display:none;"></textarea>
        </div>
    </div>

    <script>
        var editor;
        var id = "";

        function initEdit(md){
            // 编辑器设置
            editor = editormd("editorDiv", {
                // 这里的尺寸必须在这里设置. 设置样式会被 editormd 自动覆盖掉. 
                width: "100%",
                // 高度 100% 意思是和父元素一样高. 要在父元素的基础上去掉标题编辑区的高度
                height: "calc(100% - 50px)",
                // 编辑器中的初始内容
                markdown: md,
                // 指定 editor.md 依赖的插件路径
                path: "editor.md/lib/",
                saveHTMLToTextarea: true // 
            });
        }

        // 定时发布
        function time() {
            
            var releasetime = jQuery("#releasetime");
            var title = jQuery("#title");

            if (title.val() == "") {
                alert("请输入标题");
                return;
            }
            
            if (editor.getValue() == "") {
                alert("请输入文章内容");
                return;
            }
            if (releasetime.val() == "") {
                alert("请输入定时发布时间");
                return;
            }

            jQuery.ajax({
                url: '/art/releasetime',
                type: 'post',
                data: {"id": id, "title":  title.val(), "content": editor.getValue(), "releaseTime": releasetime.val()},
                success: function(result) {
                    if (result != null && result.statusCode == 200 && result.data == 1) {
                        alert("定时发布成功");
                        location.href = "blog_time.html";
                    } else {
                        alert("定时发布失败，请重试！");
                    }
                }
            });
        }

        // 保存草稿
        function save() {

            var title = jQuery("#title") 

            if(confirm("是否修改草稿？")) {
                jQuery.ajax({
                    url: '/art/save',
                    type: 'post',
                    data: {"id": id, "title": title.val(), "content": editor.getValue()},
                    success: function(result) {
                        if (result != null && result.statusCode == 200 && result.data == 1) {
                            alert("保存草稿成功");
                            location.href = location.href;
                        } else {
                            alert("保存失败，请重试");
                        }
                    }
                });
            }
        }

        // 发布
        function mysub(){
            // 非空效验
            var title = jQuery("#title") 
            if (title.val == "") {
                alert("请输入文章标题");
                return;
            }
            if (editor.getValue() == "") {
                alert("请输入文章正文");
            }
            if (confirm("是否确认发布？")) {
                jQuery.ajax({
                    url: '/art/update',
                    type: 'post',
                    data: {"id": id, "title": title.val(), "content": editor.getValue()},
                    success: function(result) {
                        if (result != null && result.statusCode == 200 && result.data == 1) {
                            alert("发布成功");
                            location.href = "/myblog_list.html";
                        } else {
                            alert("发布失败，请重试！");
                        }
                    }
                });
            }
        }

        function initArticle() {
            id = getUrlValue("id");
            if (id == "") {
                alert("无效参数");
                location.href = "/myblog_list.html";
                return;
            }
            jQuery.ajax({
                    url: '/art/detail',
                    type: 'post',
                    data: {"id": id},
                    success: function(result) {
                        if (result != null && result.statusCode == 200) {
                            jQuery("#title").val(result.data.title);
                            initEdit(result.data.content);
                        } else {
                            alert("查询失败，请重试");
                        }
                    }
                });
        }

        initArticle();

    </script>
</body>

</html>